from django.contrib import admin
from django.utils.html import format_html

from .models import Category, Product


# Register your models here.

@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    list_display = ['id', 'name', 'slug','operator']
    # prepopulated_fields用于让slug字段通过name字段自动生成
    prepopulated_fields = {'slug': ('name',)}

    def operator(self, obj):
        return format_html(f'<a href="/admin/shop/category/{obj.id}/change/">编辑</a>')

    operator.short_description = '操作'


@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    list_display = ['name', 'slug', 'price', 'available', 'created', 'updated']
    list_filter = ['available', 'created', 'updated']
    list_editable = ['price', 'available']
    # prepopulated_fields用于让slug字段通过name字段自动生成
    prepopulated_fields = {'slug': ('name',)}

    def operator(self, obj):
        return format_html(f'<a href="/admin/shop/product/{obj.id}/change/">编辑</a>')

    operator.short_description = '操作'


admin.site.site_header = 'myshop'
admin.site.site_title = '商品后台管理'
